/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parih;

import java.util.Random;

/**
 *
 * @author Drak_kin
 */
public class Person {
    public enum Status{before_arrive,arrive,wait_stri4sya, wait_strichsya_on_sofa,strishka,wait_to_pay,pay,leave}
    public Status status;
    public int number;
    public int time_arrive;
    public int time_to_end;
    public Person(int num,int time){
        number=num;
        time_arrive=time;
        status= Status.before_arrive;
        time_to_end=0;
    }
    public void change_status(Chair chair, Sofa sofa, Wait_place w_p, Pay_place p_p, int time){
        switch(status){
            case before_arrive:{
                if (time_arrive==time) {
                    status= Status.arrive;
                    System.out.print((time/60));
                    System.out.print(":");
                    System.out.print((time%60));
                    System.out.print(" клиент ");
                    System.out.print(number);
                    System.out.println(" прибыл");
                }else break;
            }
            case arrive:{
                if (w_p.is_Free()){
                    try{
                        w_p.Stand(this);
                        status= Status.wait_stri4sya;
                        System.out.print((time/60));
                        System.out.print(":");
                        System.out.print((time%60));
                        System.out.print(" клиент ");
                        System.out.print(number);
                        System.out.println(" ожидает очереди");
                        
                    }catch(Exception ex){
                        status= Status.leave;
                        System.out.print((time/60));
                        System.out.print(":");
                        System.out.print((time%60));
                        System.out.print(" клиент ");
                        System.out.print(number);
                        System.out.println(" покинул парихмахерскую, так как не было место для ожидания");
                    }
                }else{
                    status= Status.leave;
                        System.out.print((time/60));
                        System.out.print(":");
                        System.out.print((time%60));
                        System.out.print(" клиент ");
                        System.out.print(number);
                        System.out.println(" покинул парихмахерскую, так как не было мест для ожидания");
                        break;
                }
            }
            case wait_stri4sya:{
                if( sofa.is_Free()){
                    try{
                        sofa.Sit(this);
                        w_p.Leave(this);
                        status= Status.wait_strichsya_on_sofa;
                        System.out.print((time/60));
                        System.out.print(":");
                        System.out.print((time%60));
                        System.out.print(" клиент ");
                        System.out.print(number);
                        System.out.println(" ожидает на диване");
                    }catch(Exception ex){}
                }else break;
                
            }
            case wait_strichsya_on_sofa:{
                if(chair.is_Free()){
                   try{
                       status= Status.strishka;
                       chair.Sit(this);
                       sofa.Leave(this);
                       System.out.print((time/60));
                       System.out.print(":");
                       System.out.print((time%60));
                       System.out.print(" клиент ");
                       System.out.print(number);
                       System.out.println(" стрижется");
                       time_to_end=time+20+new Random().nextInt(20);
                   }catch(Exception ex){}
                } else break;
            }
            case strishka:{
                if (time==time_to_end){
                    status= Status.pay;
                    try{
                    chair.Leave(this);
                    }catch(Exception ex){}
                    System.out.print((time/60));
                    System.out.print(":");
                    System.out.print((time%60));
                    System.out.print(" клиент ");
                    System.out.print(number);
                    System.out.println(" закончил стрижку");
                    
                } else break;
            }
        }
    }
}
